/*
 * Copyright (c) 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#define ENABLE_TRACE    0
//#define ENABLE_PROFILE  1
//#define ENABLE_LOG_MACRO 0
#define TRACE_DRAW      true
#define TRACE_DIRTY     false

#include "ScreenLayerTest.h"
#include <stdio.h>

using namespace alkes;

ScreenLayerTest::ScreenLayerTest()
: screen_(0)
{
    // Enable fps option
    setOption(AL_ADO_COUNT_FPS, true);
}

ScreenLayerTest::~ScreenLayerTest()
{
    if (screen_)
    {
        delete screen_;
        screen_ = 0;
    }
}

bool ScreenLayerTest::onPreInit(int argc, char* argv[])
{
    TRACE();
    return true;
}

bool ScreenLayerTest::onInit(IGraphicsDevice* device)
{
    PROF_BEGIN("GraphicsTest::onInit");

    screen_ = new Screen(restrict_cast<IGraphics2DDevice*>(device));

    Layer* layer = screen_->registerLayer();
    layer->setClearColor(Color::BLUE);

    LOGD("onInit(%p)", device);
    PROF_END("GraphicsTest::onInit");
    return true;
}

void ScreenLayerTest::onFin()
{
    if (screen_)
    {
        delete screen_;
        screen_ = 0;
    }
    LOGD("onFin()");
}

void ScreenLayerTest::onDraw()
{
    if (screen_->update())
    {
        screen_->render();
        LOGD("Rasterize and flip.");
    }
    else
    {
        //LOGD("Skip rasterize.");
    }
}

void ScreenLayerTest::onResize(uint16_t new_width, uint16_t new_height)
{
    LOGD("onResize(%3d, %3d)", new_width, new_height);
}

void ScreenLayerTest::onDirty(const Rect& rect)
{
    screen_->addDirtyRect(rect);
    TRACE_IF(TRACE_DIRTY);
    LOGD("onDirty(%d,%d,%d,%d)", rect.x, rect.y, rect.width, rect.height);
}

void ScreenLayerTest::onPress(KeyEvent key)
{
    LOGD("onPress(0x%02x)", key);
}

void ScreenLayerTest::onRelease(KeyEvent key)
{
    LOGD("onRelease(0x%02x)", key);
}

void ScreenLayerTest::onDeviceEvent(uint16_t device_no, uint64_t value)
{
    TRACE();
}

void ScreenLayerTest::onMouseMove(uint16_t x, uint16_t y)
{
    LOGD("onMouseMove(%3d, %3d)", x, y);
}
